package com.yzy.fly.loader;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzy.fly.entity.role.*;
import com.yzy.fly.mapper.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 模型加载器
 */
@Component
public class FlyServiceLoader {
    @Autowired
    private FlyServiceMapper flyServiceMapper;
    @Autowired
    private FlyServiceMethodMapper flyServiceMethodMapper;

    public FlyService loader(int id) {
        return loaderRootId(id);
    }

    public FlyService loaderRootId(int id) {
        FlyService flyService = flyServiceMapper.selectById(id);
        return loader(flyService);
    }

    public FlyService loader(FlyService flyService) {
        if (flyService != null) {
            Integer serviceId = flyService.getId();
            QueryWrapper<FlyServiceMethod> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parentId",serviceId);
            List<FlyServiceMethod> methods = flyServiceMethodMapper.selectList(queryWrapper);
            if (CollectionUtils.isNotEmpty(methods)) {
                flyService.setMethods(methods);
            }
        }
        return flyService;
    }
}
